// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 * Creator: yanking
 * Create time: 2022-02-22 15:57
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.leetCode.DistanceBetweenBusStops;

import java.util.Arrays;

public class DistanceBetweenBusStops {
    public int distanceBetweenBusStops(int[] distance, int start, int destination) {
        // 环形公交车站距离计算
        // 因为是环形公交车站，所以一圈的距离是两条路线的距离之和
        int w1 = 0, w2 = 0;
        int n = distance.length;
        // 走第一条路径的距离
        w1 = getCost(start, destination, distance, n);
        // 走第二条路径的距离
        int sum = Arrays.stream(distance).sum();
        w2 = sum - w1;
        return Math.min(w1, w2);
    }

    public int getCost(int start, int destination, int[] distance, int n) {
        int cost = 0;
        int next = start;
        while (next != destination) {
            cost += distance[next];
            next = (next + 1) % n;
        }
        return cost;
    }

}
